import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import numpy as np

# 读取数据，设置数据
stu = [['学生5',100,90],['学生3',97,89],['学生13',88,98],['学生7',77,100],['学生2',80,96],
       ['学生12',98,76],['学生9',99,56],['学生14',88,56],['学生6',90,43],['学生11',89,32],
       ['学生8',88,32],['学生4',90,24],['学生15',88,21],['学生16',99,1],['学生1',89,11],['学生10',89,2]]

stu = pd.DataFrame(stu)
stu.columns = ['name', 'math', 'sport']
stu.index = stu['name']
stu = stu[stu.columns[1:]]

# 第一步：标准归一化处理
scaler = MinMaxScaler()  # (0,1)标准化
stu[['math', 'sport']] = scaler.fit_transform(stu[['math', 'sport']])
# 第二步：计算熵和权
# 2.1
yij = stu.apply(lambda x: x / x.sum(), axis=0)  # 第i个学生的第j个指标值的比重yij = xij/sum(xij) i=(1,m)
K = 1/np.log(len(stu))  # 常数
# 2.2
tmp = yij*np.log(yij)
tmp = np.nan_to_num(tmp)
ej = -K*(tmp.sum(axis=0))  # 计算第j个指标的信息熵
# 2.3
wj = (1 - ej) / np.sum(1 - ej)  # 计算第j个指标的权重

score = yij.apply(lambda x: np.sum(100 * x * wj), axis=1)

print(score)

# top5 = heapq.nlargest(5,score)